<?php
/** 
　* 数据库PDO操作 
  * @Copyright (C) 2020 比特福API All rights reserved.
  * @License http://tool.bitefu.com
  * @Author xiaogg <xiaogg@sina.cn>
　*/ 
class db { 
	public static $PDOStatement = null;public static $config = array();public static $pconnect = false;
	public static $error = '';protected static $link;public static $connected = false;public static $numRows = 0; 
	public static $dbVersion = null; public static $queryStr = '';public static $lastInsertId = null;
	public function connect($dbhost, $dbuser, $dbpw, $dbname,$dbprot=3306,$dbtype='mysql',$db_charset='utf8'){ 
		if (!class_exists('PDO')) self::throw_exception("不支持:PDO");if($dbprot<3000)$dbprot=3306;
		$dbConfig = array('hostname' => $dbhost,'username' => $dbuser,'password' => $dbpw,'database' => $dbname,'hostport' => $dbprot,'dbms' => $dbtype,'dsn' => $dbtype.":host=".$dbhost.";dbname=".$dbname);
		if(empty($dbConfig['hostname'])) self::throw_exception("没有定义数据库配置"); 
		self::$config = $dbConfig;if(empty(self::$config['params'])) self::$config['params'] = array();
		if (!isset(self::$link) ) {$configs = self::$config;if(self::$pconnect) {$configs['params'][constant('PDO::ATTR_PERSISTENT')] = true;} 
			try {self::$link = new PDO( $configs['dsn'], $configs['username'], $configs['password'],$configs['params']);} catch (PDOException $e) { self::throw_exception($e->getMessage());} 
			if(!self::$link) {self::throw_exception('PDO CONNECT ERROR'); return false;}self::$link->exec('SET NAMES '.$db_charset); 
			self::$dbVersion = self::$link->getAttribute(constant("PDO::ATTR_SERVER_INFO")); self::$connected = true;unset($configs); 
		}return self::$link;}
	public static function get_all($sql=null) {if($sql != null){self::select($sql);}return self::$PDOStatement->fetchAll(constant('PDO::FETCH_ASSOC'));}
	public static function get_one($sql=null) {if($sql != null){self::select($sql);}return self::$PDOStatement->fetch(constant('PDO::FETCH_ASSOC'),constant('PDO::FETCH_ORI_NEXT'));}
	public static function query($sql='') {if(self::isMainIps($sql)) {return self::execute($sql);}else{return self::get_all($sql);}}	
	public static function getLastSql(){$link = self::$link; if ( !$link ) return false; return self::$queryStr;} 
	public static function getLastInsId(){$link = self::$link; if ( !$link ) return false; return self::$lastInsertId; } 
	public static function getDbVersion(){$link = self::$link; if ( !$link ) return false; return self::$dbVersion; } 
	public static function close() {self::$link = null; }public static function free() {self::$PDOStatement = null;}
	public static function select($sql='') {$link = self::$link; if ( !$link ) return false;self::$queryStr = $sql; if ( !empty(self::$PDOStatement) ) self::free();self::$PDOStatement = $link->prepare(self::$queryStr);$bol = self::$PDOStatement->execute();self::haveErrorThrowException(); return $bol;}
	public static function execute($sql='') {$link = self::$link;if ( !$link ) return false;self::$queryStr = $sql;if ( !empty(self::$PDOStatement) ) self::free();$result = $link->exec(self::$queryStr);self::haveErrorThrowException(); 
		if ( false === $result) { return false; } else { self::$numRows = $result; self::$lastInsertId = $link->lastInsertId(); return self::$numRows; }}
	private static function ismainIps($query) {$queryIps = 'INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|SELECT .* INTO|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK'; if (preg_match('/^\s*"?(' . $queryIps . ')\s+/i', $query)) { return true; } return false; } 
	public static function throw_exception($err){echo 'ERROR:'.$err.'';}
	public static function haveErrorThrowException() {$obj = empty(self::$PDOStatement) ? self::$link : self::$PDOStatement;$arrError = $obj->errorInfo(); 
		if($arrError[0] !== '00000') {self::$error = $arrError[0]."|".$arrError[2]. "<br/>[ SQL ] : ".self::$queryStr."<br/>"; self::throw_exception(self::$error); return false;}
		if(self::$queryStr=='')self::throw_exception('Query was empty<br/><br/>[ SQL语句 ] :'); 
	}
}?>